package com.bestv.search.engine.command;

import com.bestv.search.common.model.History;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Michael Wang
 */

// $Id: HistoryQueue.java 29 2012-03-13 03:37:49Z wbwang $
public class HistoryQueue {

    private static final int DEFAULT_HISTORY_LIMIT = 10;
    protected final Log log = LogFactory.getLog(getClass());
    private boolean firstLevelLock = false;
    private Map<String, History> firstLevelMap = new HashMap<String, History>();
    private Map<String, History> secondLevelMap = new HashMap<String, History>();

    public void push(History history) {
        if (firstLevelLock) {
            log.debug("First level map locked, push history " + history + " into second level map.");
            doPush(history, secondLevelMap);
        } else {
            if (secondLevelMap.size() > 0) {
                firstLevelMap.putAll(secondLevelMap);
                secondLevelMap.clear();
                log.debug("Second level history map is clear.");
            }
            doPush(history, firstLevelMap);
        }
    }

    public History get(String userId) {
        if (firstLevelLock) {
            return secondLevelMap.get(userId);
        } else {
            return firstLevelMap.get(userId);
        }
    }

    public String getUserHistory(String userId) {
        History history = get(userId);
        return history != null ? history.getHistory() : "";
    }

    public void doPush(History history, Map<String, History> map) {
        String userId = history.getUserId();
        if (map.containsKey(userId)) {
            History latestHistory = map.get(userId);
            latestHistory.setTime(history.getTime());
            String historyStr = latestHistory.getHistory();
            if (historyStr.indexOf("_") == -1) {
                if (!historyStr.equals(history.getHistory().trim()))
                    historyStr = history.getHistory().trim() + "_" + historyStr;
            } else {
                String[] historyArr = historyStr.split("_");
                if (!historyArr[0].equals(history.getHistory().trim())) {
                    if (historyArr.length >= DEFAULT_HISTORY_LIMIT) {
                        historyStr = history.getHistory().trim() + "_"
                                + historyStr.substring(0, historyStr.lastIndexOf("_"));
                    } else {
                        historyStr = history.getHistory().trim() + "_" + historyStr.trim();
                    }
                }
            }
            latestHistory.setHistory(historyStr);
            map.put(userId, latestHistory);
        } else {
            map.put(userId, history);
        }
    }

    public boolean isFirstLevelLock() {
        return firstLevelLock;
    }

    public void setFirstLevelLock(boolean firstLevelLock) {
        this.firstLevelLock = firstLevelLock;
    }

    public Map<String, History> getFirstLevelMap() {
        return firstLevelMap;
    }

    public void setFirstLevelMap(Map<String, History> firstLevelMap) {
        this.firstLevelMap = firstLevelMap;
    }

    public void clear() {
        this.firstLevelMap.clear();
    }
}
